package com.hlhj.carview.aty

import android.app.AlertDialog
import android.content.Intent
import android.net.Uri
import android.view.View
import com.bumptech.glide.Glide
import com.example.mymvp.log
import com.hlhj.carview.BaseAty
import com.hlhj.carview.BaseAtyContainer
import com.hlhj.carview.Constant
import com.hlhj.carview.Iview.UploadView
import com.hlhj.carview.Iview.UserView
import com.hlhj.carview.R
import com.hlhj.carview.bean.IconBea
import com.hlhj.carview.bean.MyBaseBean
import com.hlhj.carview.bean.UserInfoBean
import com.hlhj.carview.customView.DilaogPrivacy
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.MyJsonConvert
import com.hlhj.carview.http.Urls
import com.hlhj.carview.presenter.UploadPresenter
import com.hlhj.carview.presenter.UserPresetner
import com.hlhj.carview.utils.GlideUtil
import com.hlhj.carview.utils.MyGlideEngin
import com.hlhj.carview.utils.SPHelper
import com.huantansheng.easyphotos.EasyPhotos
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.ObservableResponse
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.aty_login.*
import kotlinx.android.synthetic.main.aty_user.*
import kotlinx.android.synthetic.main.aty_user.btPrivacy
import java.io.File

/**
 * Created by Never Fear   on 2018\12\18 0018.
Never More....
 */
class UserAty :BaseAty<UserView,UserPresetner>(), UserView, UploadView {
    private var headFile:File?=null
    private var uploadPresenter:UploadPresenter?=null

    override fun setUser(userInfoBean: UserInfoBean) {
        val info = userInfoBean.info
        userName.text= "${info.name}"
        Glide.with(this).load(Urls.Img+info.head_img).apply(GlideUtil.loadHeadOption())
                .into(userIcon)

        clientMoney.text="￥${info.balance}"
        SPHelper.saveHead(Urls.Img+info.head_img,this)
        when(info.sex){
            0->{
                userSex.visibility=View.GONE
            }
            1->{
                userSex.visibility=View.VISIBLE
                userSex.setImageResource(R.drawable.ic_grzx_nan)
            }
            2->{
                userSex.visibility=View.VISIBLE
                userSex.setImageResource(R.drawable.ic_grzx_nv)
            }
        }
        if (info?.frozen==1){
            isAVliable.text="已冻结"
        }else{
            isAVliable.text="可提现"
        }
        when(info.authentication){
            0->{
                btIdentity.text="未认证"
                btIdentity.setOnClickListener {
                    val intent = Intent(this, IdentityAty::class.java)
                    startActivityForResult(intent,REFRESH)
                }
            }
            1->{
                btIdentity.text="待审核"
                btIdentity.setOnClickListener {

                }
            }
            2->{
                btIdentity.text="已通过"
                btIdentity.setOnClickListener {

                }
            }
        }
    }

    override fun setChangeResult(myBaseBean: MyBaseBean) {
        Glide.with(this).load(headFile).into(userIcon)
        setResult(-1)
        SPHelper.saveHead(headFile?.absolutePath!!,this)
    }
    override fun setUpResult(myBaseBean: MyBaseBean) {
        presenter?.changeHead(myBaseBean.info.toString(),this)
    }

    override fun initView() {
        userPhone.text=SPHelper.getToken(this)
    }

    override fun logicStart() {
    }

    private val TAKEPHOTO2: Int=1010
    private val REFRESH=111
    override fun setListener() {
        userIcon.setOnClickListener {
        }

        btWallet.setOnClickListener {
            val intent = Intent(this, WalletAty::class.java)
            startActivityForResult(intent,REFRESH)
        }
        btCarInfo.setOnClickListener {
            val intent = Intent(this, CarInfoAty::class.java)
            startActivity(intent)
        }
        btEquipment.setOnClickListener {
            val intent = Intent(this, AskEquipmentAty::class.java)
            startActivity(intent)
        }
        btAboutUs.setOnClickListener {
            val intent = Intent(this, AboutUsAty::class.java)
            startActivity(intent)
        }
        btReport.setOnClickListener {
            val intent = Intent(this, ReportAty::class.java)
            startActivity(intent)
        }
        btServer.setOnClickListener {
            // 打电话
            // Intent : 意图.我想去做一件事
            val t = Intent()
            // Action:动作.我具体想做什么事
            // Intent.ACTION_DIAL: 激活拨号界面
            // Intent.ACTION_CALL: 直接拨打电话
            t.action = Intent.ACTION_CALL
            // Data: 数据.具体的动作所需要的附加数据
            //封装一个拨打电话的intent，并且将电话号码包装成一个Uri对象传入
            t.data = Uri.parse("tel:" + Constant.phone)
            // 通知系统你去帮我干活吧
            startActivity(t)
        }
        btLoginOut.setOnClickListener {
            AlertDialog.Builder(this)
                    .setTitle("确认退出？")
                    .setPositiveButton("确定"){
                        dialog, which ->
                        SPHelper.saveLogin(false,this)
                        BaseAtyContainer.instance.finishAllActivity()
                        startActivity(Intent(this,LoginAty::class.java))
                    }
                    .setNegativeButton("取消"){
                        dialog, which ->
                        dialog.dismiss()
                    }
                    .show()
        }
        btPrivacy.setOnClickListener {
            DilaogPrivacy(this).show()
        }
    }

    override fun setUpResult(myBaseBean: MyBaseBean, tag: Int) {
        //无用
    }
    private var head:String?=null
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        if (requestCode==TAKEPHOTO2){
            val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
            headFile=File(pathList[0])
            uploadPresenter?.upload(headFile!!,this)
        }
        if (requestCode==REFRESH){
            presenter?.getUser(this@UserAty)
        }
    }
    override fun bindView(): UserView {
        return this
    }

    override fun createPresenter(): UserPresetner {
        uploadPresenter= UploadPresenter()
        uploadPresenter?.let {
            it.createModule()
            it.bindView(this@UserAty)
        }
        return UserPresetner()
    }

    override fun getContentID(): Int {
        return R.layout.aty_user
    }
}